<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" @click="handleAdd()" type="primary" icon="el-icon-plus">添加</el-button>
      <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;"  placeholder="请输入关键词" v-model="search.keyword"></el-input>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table
       v-loading="!pload"
       @selection-change = "selectionChange"
        ref="multipleTable"
        border
        :data="tableData"
        :cell-class-name="setClass"
       >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="authUser.userAccount"
          label="帐号"
          align="center"
          show-overflow-tooltip
          >
        </el-table-column>
        <el-table-column
          property="authUser.userName"
          label="名字"
          align="center"
          show-overflow-tooltip
          >
        </el-table-column>
        <el-table-column
          label="角色"
          align="center"
          show-overflow-tooltip
          >
          <template slot-scope="scope">
            {{scope.row.authRoleList.length && scope.row.authRoleList[0].roleName }}
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          property="authUser.comment"
          label="备注"
          >
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
          width="100"
          >
          <template slot-scope="scope">
            <el-tag v-if="!scope.row.authUser.userStatus" type="success" size="mini">启用</el-tag>
            <el-tag v-else type="danger" size="mini">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="分配"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-badge :value="scope.row.siteList && scope.row.siteList.length" class="item" v-if="scope.row.authUser.userId > 1">
              <el-button size="mini" @click="handleSite(scope.$index, scope.row)">分配站点</el-button>
            </el-badge>
            <span v-else>全部站点</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" :disabled="scope.row.authUser.userId < 2">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <div class="filter-bottom">
      <el-button-group class="filter-item">
        <el-button @click="toggleSelection(-1)">全选</el-button>
        <el-button @click="toggleSelection(0)" :disabled="!selection.length">反向选择</el-button>
        <el-button @click="toggleSelection(1)" :disabled="!selection.length">取消选择</el-button>
        <el-button @click="toggleDel" :disabled="!selection.length">删除</el-button>
      </el-button-group>
    </div>
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
      :page-size="limit"
      :page-sizes="limits"
      background
      layout="sizes, prev, pager, next, jumper, total"
      :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :data="selectTable" @getData="getData"></edit>
    <userAssignSite v-if="userAssignSite" :data="selectTable" @getData="getData"></userAssignSite>
  </div>
</template>

<script>
    import {userList,userRemove} from '@/utils/api'
    import waves from '@/directive/waves' // 水波纹指令
    import edit from './edit'
    import userAssignSite from './assignSite'
    export default {
      directives: {
        waves
      },
      data(){
          return {
            tableData: [],
            search: {
              keyword: ''
            },
            selection: [],
            selectTable: {},
            pload: false,
            edit: false,
            userAssignSite: false,
            currentPage:1,
            limit: 20,
            limits: [10,20,50,100],
            count:0,
          }
      },
      components: {
        edit,
        userAssignSite
      },
      created(){
         this.getData()
      },
      methods: {
        selectionChange (selection) {
          this.selection = selection
        },
        toggleSelection(s) {
          if (s===-1) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row,true)
            })
          }
          if (s===0) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row)
            })
          }
          if (s===1) {
            this.$refs.multipleTable.clearSelection()
          }
        },
        handleAdd () {
          this.selectTable = {}
          this.selectTable.method = 'add'
          this.edit = true
        },
        async handleDelete(index, row) {
          this.$confirm('确认要删除吗',{type:'warning'}).then(async () => {
            try{
              this.pload = false;
              await userRemove({uids: row.authUser.userId})
              this.$notify({title: '删除成功', type: 'success', duration: 5000})
              this.getData()
            } catch(e) {
              this.pload = true;
            }
          })
        },
        toggleDel () {
          this.$confirm('确认要删除吗',{type:'warning'}).then(async () => {
            let ids = this.selection.map((i,index) => {
              return i.authUser.userId
            }).join(',')
            try{
              this.pload = false;
              await userRemove({uids: ids})
              this.$notify({title: '删除成功', type: 'success', duration: 5000})
              this.getData()
            } catch(e) {
              this.pload = true;
            }
          })
        },
        handleEdit(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.selectTable.method = 'edit'
          this.edit = true
        },
        handleSite(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.userAssignSite = true
        },
        async getData(){
          this.pload = false
          const Datas = await userList({
            pageNum: this.currentPage,
            pageSize: this.limit,
            keyword: this.search.keyword
          })
          this.pload = true
          this.tableData = Datas.data
          this.count = Datas.pageInfo.total
        },
        handleFilter () {
          this.getData()
        },
        handleSizeChange(val) {
          console.log(`每页 ${val} 条`)
          this.limit = val
          this.getData()
        },
        handleCurrentChange(val) {
          this.currentPage = val
          this.getData()
        },
        setClass(obj) {
          if(obj.column.label==='分配') {
            return 'table-auto'
          }
        }
      }
    }
</script>

<style>
  .table-auto .cell {
    overflow: inherit;
  }
</style>
